<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath(); //解決跳轉后樣式丟失的問題
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css"
	href="<%=path%>/background/style/resource/Css/bootstrap.css" />
<link rel="stylesheet" type="text/css"
	href="<%=path%>/background/style/resource/Css/bootstrap-responsive.css" />
<link rel="stylesheet" type="text/css"
	href="<%=path%>/background/style/resource/Css/style.css" />
<script type="text/javascript"
	src="<%=path%>/background/style/resource/Js/jquery.js"></script>
<script type="text/javascript"
	src="<%=path%>/background/style/resource/Js/jquery.sorted.js"></script>
<script type="text/javascript"
	src="<%=path%>/background/style/resource/Js/bootstrap.js"></script>
<script type="text/javascript"
	src="<%=path%>/background/style/resource/Js/ckform.js"></script>
<script type="text/javascript"
	src="<%=path%>/background/style/resource/Js/common.js"></script>

<link href="<%=path%>/background/business/pagination/asset/normalize/normalize.css" type="text/css"
	rel="stylesheet" media="all">
<link href="<%=path%>/background/business/pagination/css/layout.css" type="text/css" rel="stylesheet">
<link href="<%=path%>/background/business/pagination/css/autoc.min.css" type="text/css"
	rel="stylesheet">
<link href="<%=path%>/background/business/pagination/css/pagination.css" type="text/css"
	rel="stylesheet">
<script type="text/javascript" src="<%=path%>/background/business/pagination/asset/jquery/jquery.js"></script>
<script type="text/javascript" src="<%=path%>/background/business/pagination/js/autoc.js"></script>
<script type="text/javascript" src="<%=path%>/background/business/pagination/js/pagination.js"></script>

<style type="text/css">
body {
	padding-bottom: 40px;
}

.sidebar-nav {
	padding: 9px 0;
}

@media ( max-width : 980px) {
	/* Enable use of floated navbar text */
	.navbar-text.pull-right {
		float: none;
		padding-left: 5px;
		padding-right: 5px;
	}
}
</style>
</head>
<body>
	<form class="form-inline definewidth m20">
		商品名/ID： <input type="text" name="goodsname" id="goodsname"
			class="abc input-default" placeholder=""  value="">&nbsp;&nbsp;
		<button type="button" class="btn btn-primary" onclick="selectByName()">名称查询</button>
		&nbsp;&nbsp;
		<button type="button" class="btn btn-primary" onclick="selectById()">ID查询</button>
		&nbsp;&nbsp;
		<button type="button" class="btn btn-success" id="addnew">新增商品</button>
	</form>
	<table class="table table-bordered table-hover definewidth m10">
		<thead>
			<tr>
				<th>商品ID</th>
				<th>商品名</th>
				<th>价格</th>
				<th>数量</th>
				<th>进货时间</th>
				<th>管理操作</th>
			</tr>
		</thead>
		<tbody id="tab1">
			
		</tbody>

	</table>
	${msg }
	<a id="pageIndex" hidden="hidden">${param.pageIndex}</a>
	<!-- class="inline pull-right page" -->
	<div  id="div2">
	</div>
</body>
</html>

<script>

	var url = "/tbpoject/business2/";
	var pageCount=1;
	var totalpage=1;
	
	$(function() {
		$('#addnew')
				.click(
						function() {
							window.location.href = "/tbpoject/background/business/addgoods.jsp";
						});
		
		loadpageCount();
		
		if($("#pageIndex").html()==null||$("#pageIndex").html()==""){
			getAllGoods(1,10);
		}else{
			getAllGoods(parseInt($("#pageIndex").html()),10);
		}
	
	});
	
	
	function loadpageCount(){
		
		$.get(url+"getgoodscount",{},function(data){
		
			pageCount=parseInt(data);
			totalpage=Math.ceil(pageCount/10);
			
			loadPagination();
		})
	} 
function loadPagination() {
		
		var Pager = new Pagination({
			parent : '#div2',
			totalPage : totalpage,
			prevText : "上一页",
			nextText : "下一页",
			pageSize : 5
		});

		Pager.on('afterChange', function(args) {
			var index = args.currentPage
			getAllGoods(index, 10);
		});
	}
	
	function getAllGoods(pageIndex, pageSize) {
		$
				.getJSON(
						url + "getallgoods",
						{"pageIndex" : pageIndex,
							"pageSize" : pageSize},
						function(data) {
							if(data!=""&&data!=null){
								var tab = $("#tab1")
								var msg = "";
								tab.html("");
								for (var i = 0; i < data.length; i++) {
									msg += "<tr>";
									msg += "<td>" + data[i].id + "</td>";
									msg += "<td>" + data[i].name + "</td>";
									msg += "<td>" + data[i].price + "</td>";
									msg += "<td>" + data[i].count + "</td>";
									msg += "<td>" + data[i].time + "</td>";
									msg += "<td><a href='javascript:del("
											+ data[i].id
											+ ")'>删除</a>&nbsp;&nbsp;<a href='/tbpoject/business2/getgoodsbyid?id="
											+ data[i].id + "'>修改</a></td>";
									msg += "</tr>";
								}
								tab.html(tab.html() + msg);
							}else{
								setPageHidden();
								
							}
							

						});

	}
	
	function setPageHidden(){
		var div = $('#div2');
		div.prop("style","display:none;");
		$("#tab1").html("无数据！");
	}
	
	//通过商品名称查找商品
	function selectByName() {
		var name = $("#goodsname").val();
		$
				.post(
						url + "selectgoodsbyname",
						{
							"name" : name
						},
						function(info) {
							
							var data = $.parseJSON(info);
							if(data!=""&&data!=null){
							var tab = $("#tab1");
							var msg = "";
							tab.html("");
							for (var i = 0; i < data.length; i++) {
								msg += "<tr>";
								msg += "<td>" + data[i].id + "</td>";
								msg += "<td>" + data[i].name + "</td>";
								msg += "<td>" + data[i].price + "</td>";
								msg += "<td>" + data[i].count + "</td>";
								msg += "<td>" + data[i].time + "</td>";
								msg += "<td><a href='javascript:del("
										+ data[i].id
										+ ")'>删除</a>&nbsp;&nbsp;<a href='/tbpoject/business2/getgoodsbyid?id="
										+ data[i].id + "'>修改</a></td>";
								msg += "</tr>";
							}
							tab.html(tab.html() + msg);
						}else{
							setPageHidden();
							
						}
						})
	}
	
	//根据商品id来查询商品
	function selectById() {
		var id = $("#goodsname").val();
		$
				.getJSON(
						url + "selectgoodsbyid",
						{
							"id" : id
						},
						function(data) {
							if(data!=""&&data!=null){
							var tab = $("#tab1")
							var msg = "";
							tab.html("");

							msg += "<tr>";
							msg += "<td>" + data.id + "</td>";
							msg += "<td>" + data.name + "</td>";
							msg += "<td>" + data.price + "</td>";
							msg += "<td>" + data.count + "</td>";
							msg += "<td>" + data.time + "</td>";
							msg += "<td><a href='javascript:del("
									+ data.id
									+ ")'>删除</a>&nbsp;&nbsp;<a href='/tbpoject/business2/getgoodsbyid?id="
									+ data.id + "'>修改</a></td>";
							msg += "</tr>";

							tab.html(tab.html() + msg);
						}else{
							setPageHidden();
						}
						})
	}
	function del(id) {
		if (confirm("确定要删除吗？")) {
			$.post(url + "deletegoods", {
				"id" : id
			}, function(data) {
				if($("#pageIndex").html()==null||$("#pageIndex").html()==""){
		            getAllGoods(1,10);
		        }else{
		            getAllGoods(parseInt($("#pageIndex").html()),10);
		        }
			})
		}
	}

	
</script>